﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyKindle
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            MaximumSize = Size;
            MinimumSize = Size;
        }

        string book;
        string kindleadress;
        string postadress;
        string postpwd;

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text) || string.IsNullOrWhiteSpace(textBox3.Text) || string.IsNullOrEmpty(book)) 
            {
                return;
            }
            kindleadress = textBox1.Text + "@kindle.cn";
            postadress = textBox2.Text;
            postpwd = textBox3.Text;
            if (!Kindle.InvadidateKindleAdress(kindleadress))
            {
                MessageBox.Show("输入的Kindle邮箱不是有效的Kindle电子邮箱!");
            }
            if (!Kindle.InvadidateEmailAdress(postadress))
            {
                MessageBox.Show("输入的发送邮箱不是有效的电子邮箱!");
            }
            var info = new Kindle.PostInfo()
            {
                BookPath = book,
                KindleAdress = kindleadress,
                PostAdress = postadress,
                PostPassWord = postpwd
            };
            var sucsess = Kindle.Send(info);
            if (sucsess)
            {
                MessageBox.Show("推送成功，请同步您的Kindle！", "EasyKindle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("推送失败，请检查您的邮件地址设置！", "EasyKindle", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = Kindle.FileFilterString;
            openFileDialog1.FileName = "";
            openFileDialog1.Title = "选择图书文件";
            var result = openFileDialog1.ShowDialog();
            if(result== DialogResult.OK)
            {
                book = openFileDialog1.FileName;
                label6.Text = "即将推送:" + openFileDialog1.SafeFileName;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://cn.epubee.com/");
        }
    }
}
